package com.choosefine.it.team.controller;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.team.model.dto.TeamCooperateBusDTO;
import com.choosefine.it.team.model.dto.TeamCooperateBusSaveDTO;
import com.choosefine.it.team.model.vo.TeamCooperateBusVO;
import com.choosefine.it.team.model.vo.TeamSupplierInfoVO;
import com.choosefine.it.team.model.vo.queryAffiliatedUnitsVO;
import com.choosefine.it.team.model.vo.queryTeamVO;
import com.choosefine.it.team.service.TeamCooperateBusService;
import com.github.pagehelper.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Comments：班组合作商家相关
 * Author：maojun
 * Create Date：2017/2/17
 * Modified By：
 * Modified Date：
 * Why & What is modified：
 * Version：v1.0
 */
@RestController
@RequestMapping("/team/teamCooperateBus")
@Api(value = "控制台—项目管理—班组合作商家相关", description = "毛俊")
public class TeamCooperateBusController extends BaseController {
    Logger logger = LoggerFactory.getLogger(TeamCooperateBusController.class);

    @Autowired
    TeamCooperateBusService teamCooperateBusService;

    /**
     * @Author maojun
     * @Date 2017/2/17 15:36
     */
    @ApiOperation(value = "查询班组合作商家", response = TeamCooperateBusVO.class)
    @RequestMapping(value = "/queryTeamCooperateBus", method = RequestMethod.POST, produces = "application/json")
    public String queryTeamCooperateBus(@RequestBody TeamCooperateBusDTO teamCooperateBusDTO) throws Exception {
        String result = null;
        Map<String, Object> params = new HashMap<>();
        if (teamCooperateBusDTO.getCompanyName() != null && !"".equals(teamCooperateBusDTO.getCompanyName())) {
            params.put("companyName", teamCooperateBusDTO.getCompanyName().trim());
        }
        if (teamCooperateBusDTO.getAffiliatedUnitId() != null && teamCooperateBusDTO.getAffiliatedUnitId()>0) {
            params.put("affiliatedUnitId", teamCooperateBusDTO.getAffiliatedUnitId());
        }
        if (teamCooperateBusDTO.getProjectCode() != null && !"".equals(teamCooperateBusDTO.getProjectCode())) {
            params.put("projectCode", teamCooperateBusDTO.getProjectCode().trim());
        }
        if(teamCooperateBusDTO.getCorporation() !=null && !"".equals(teamCooperateBusDTO.getCorporation())){
            params.put("corporation",teamCooperateBusDTO.getCorporation().trim());
        }
        if (teamCooperateBusDTO.getTenantRole() != null && !"".equals(teamCooperateBusDTO.getTenantRole())) {
            params.put("tenantRole", teamCooperateBusDTO.getTenantRole());
        }
        if (teamCooperateBusDTO.getUserCode() != null && !"".equals(teamCooperateBusDTO.getUserCode())) {
            params.put("userCode", teamCooperateBusDTO.getUserCode().trim());
        }
        PageDTO pageDto = new PageDTO();
        pageDto.setPageNum(teamCooperateBusDTO.getPageNum());
        pageDto.setPageSize(teamCooperateBusDTO.getPageSize());
        Page<TeamCooperateBusVO> teamCooperateBusVOList = teamCooperateBusService.queryTeamCooperateBus(params, pageDto);
        if (teamCooperateBusVOList.size() > 0) {
            result = responseSuccess(teamCooperateBusVOList.toPageInfo());
        } else {
            result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/17 15:36
     */
    @ApiOperation(value = "班组合作商家—选择租户角色", response = TeamSupplierInfoVO.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "supplierType", value = "供应商类型(6:材料供应商；7:设备供应商；8:设施供应商)", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "codeOrName", value = "供应商租户编号或企业名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "affiliatedUnitId", value = "分包单位id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "起始页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", required = true, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/querySupplier", method = RequestMethod.GET, produces = "application/json")
    public String querySupplier(Integer supplierType,
                                String codeOrName,
                                @RequestParam Integer affiliatedUnitId,
                                @RequestParam String projectCode,
                                @RequestParam int pageNum,
                                @RequestParam int pageSize) throws Exception {
        String result = null;
        Map<String, Object> params = new HashMap<>();
        if (supplierType != null) {
            params.put("supplierType", supplierType);
        }
        if (affiliatedUnitId >0) {
            params.put("affiliatedUnitId", affiliatedUnitId);
        }
        if (projectCode != null) {
            params.put("projectCode", projectCode);
        }
        if (codeOrName != null && !"".equals(codeOrName)) {
            params.put("codeOrName", codeOrName.trim());
        }
        PageDTO pageDto = new PageDTO();
        pageDto.setPageNum(pageNum);
        pageDto.setPageSize(pageSize);
        Page<TeamSupplierInfoVO> teamSupplierInfoVOList = teamCooperateBusService.querySupplier(params, pageDto);
        if (teamSupplierInfoVOList.size() > 0) {
            result = responseSuccess(teamSupplierInfoVOList.toPageInfo());
        } else {
            result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/17 15:36
     */
    @ApiOperation(value = "班组合作商家——解除/恢复项目合作商家关系")
    @ApiImplicitParams({@ApiImplicitParam(name = "ides", value = "班组合作商家关系id，多个id用逗号隔开", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "isValid", value = "0:恢复合作商家关系，1:解除合作商家关系", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/changePartnership/{ides}/{isValid}", method = RequestMethod.GET, produces = "application/json")
    public String changePartnership(@PathVariable String ides, @PathVariable Integer isValid) {
        String result=null;
        String[] ids = ides.split(",");
        Map<String, Object> params = new HashMap<>();
        params.put("utime", DateUtils.getNowTime());
        params.put("ids", ids);
        params.put("isValid", isValid);
        Integer num = teamCooperateBusService.changePartnership(params);
        if (num != null && num > 0) {
            result = responseSuccess(num, ResultCode.SUCCESS_CREATED, ResultCode.MSG_UPDATE_SUCCESS);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/17 15:36
     */
    @ApiOperation(value = "班组合作商家—新增项目合作商家")
    @RequestMapping(value = "/saveTeamCooperateBus", method = RequestMethod.POST, produces = "application/json")
    public String saveTeamCooperateBus(@RequestBody List<TeamCooperateBusSaveDTO> list) {
        String result = null;
        Integer num = teamCooperateBusService.saveTeamCooperateBus(list);
        if (num != null && num > 0) {
            result = responseSuccess(num, ResultCode.SUCCESS_CREATED, ResultCode.MSG_CREATE_SUCCESS);
        } else {
            result = responseFail(ResultCode.MSG_ERROR);
        }
        return result;
    }

    /**
     * @Author maojun
     * @Date 2017/2/17 15:36
     */
    @ApiIgnore
    @ApiOperation(value = "班组合作商家—查询班组", response = queryTeamVO.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "teamName", value = "分包承包人姓名模糊查询", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", required = false, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/queryTeam", method = RequestMethod.GET, produces = "application/json")
    public String queryTeam(String teamName,String projectCode) throws Exception {
        String result = null;
        Map<String, Object> params = new HashMap<>();
        if (teamName != null) {
            params.put("teamName", teamName);
        }
        if(projectCode != null){
            params.put("projectCode",projectCode);
        }
        List<queryTeamVO> queryTeamVOList = teamCooperateBusService.queryTeam(params);
        if (queryTeamVOList.size() > 0) {
            result = responseArraySuccess(queryTeamVOList);
        } else {
            result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
        }
        return result;
    }
    @ApiOperation(value = "班组合作商家—选择分包单位", response = queryAffiliatedUnitsVO.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "companyCodeOrName", value = "分包单位租户编号或企业名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", required = false, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/queryAffiliatedUnits", method = RequestMethod.GET, produces = "application/json")
    public String queryAffiliatedUnits(String companyCodeOrName,String projectCode) throws Exception {
        String result = null;
        Map<String, Object> params = new HashMap<>();
        if (companyCodeOrName != null) {
            params.put("companyCodeOrName", companyCodeOrName);
        }
        if(projectCode != null){
            params.put("projectCode",projectCode);
        }
        List<queryAffiliatedUnitsVO> list = teamCooperateBusService.queryAffiliatedUnits(params);
        if (list.size() > 0) {
            result = responseArraySuccess(list);
        } else {
            result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
        }
        return result;
    }
}